package com.wx.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wx.entity.SmbmsRole;
import com.wx.entity.SmbmsUser;
import com.wx.service.SmbmsRoleService;
import com.wx.service.SmbmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class SmbmsUserController {
    @Autowired
    HttpSession httpSession;
    @Autowired
    SmbmsUserService smbmsUserService;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("/jsp/validatePwd.do")
    @ResponseBody
    public HashMap validatePwd(String oldpassword) {
        //redis
        // 缓存， 在内存当中的都叫缓存             内存当中的数据读写块
        // 数据库 文件，持久化的数据  读 缓存

        SmbmsUser smbmsUser = (SmbmsUser)  redisTemplate.opsForValue().get("userSession");
        HashMap<String, String> hashMap = new HashMap<>();
        if (smbmsUser.getUserPassword().equals(oldpassword)) {
            hashMap.put("result", "true");
        } else {
            hashMap.put("result", "false");
        }
        return hashMap;
    }

    @RequestMapping("/jsp/updPwd.do")
    public String updPwd(String rnewpassword) {
        SmbmsUser smbmsUser = (SmbmsUser)  redisTemplate.opsForValue().get("userSession");
        smbmsUser.setUserPassword(rnewpassword);
        smbmsUserService.updSmbmsUser(smbmsUser);
        return "pwdmodify";
    }

    @Autowired
    SmbmsRoleService smbmsRoleService;

    @RequestMapping("/jsp/userlist.do")
    public String userList(String pageIndex,String queryname,String queryUserRole){
        SmbmsUser smbmsUser = new SmbmsUser();

        smbmsUser.setUserName(queryname);
        queryUserRole=queryUserRole==null?"0":queryUserRole;
        smbmsUser.setUserRole(Integer.valueOf(queryUserRole));
        pageIndex=pageIndex==null?"1":pageIndex;
        smbmsUser.setCurrentPage(Integer.valueOf(pageIndex));
        IPage<SmbmsUser>  smbmsUserIPage =  smbmsUserService.getSmbmsUserByPage(smbmsUser);
        List<SmbmsRole> smbmsRoleAll = smbmsRoleService.getSmbmsRoleAll();
        httpSession.setAttribute("roleList",smbmsRoleAll);
        httpSession.setAttribute("userList",smbmsUserIPage.getRecords());
        httpSession.setAttribute("queryUserName",queryname);
        httpSession.setAttribute("queryUserRole",Integer.valueOf(queryUserRole));

        httpSession.setAttribute("totalPageCount",smbmsUserIPage.getPages());
        httpSession.setAttribute("totalCount",smbmsUserIPage.getTotal());
        httpSession.setAttribute("currentPageNo",smbmsUserIPage.getCurrent());
        return "userlist";
    }

    @RequestMapping("/jsp/getrolelist.do")
    @ResponseBody
    public List<SmbmsRole> getrolelist() {
        return smbmsRoleService.getSmbmsRoleAll();
    }
    @RequestMapping("/jsp/adduser.do")
    public String adduser(SmbmsUser smbmsUser){
        //对象  ===>HashMap
        smbmsUser.setCreationDate(new Date());
        smbmsUserService.addSmbmsUser(smbmsUser);
        return   "redirect:/jsp/userlist.do";
    }
    @RequestMapping("jsp/userexist.do")
    @ResponseBody
    public HashMap<String, String> userexist(String userCode) {
        System.out.println(userCode);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("userCode", "exist");
        SmbmsUser smbmsUser = smbmsUserService.getSmbmsUserByUserCode(userCode);
        if (smbmsUser == null) {
            resultMap.put("userCode", "noexist");
        }
        return resultMap;
    }


    @RequestMapping("/jsp/deluser.do")
    @ResponseBody
    public HashMap<String, String> delUser(String uid) {
        //403 权限
        //404 页面没找到
        //400 参数问题
        //500  java
        System.out.println(uid);
        //调用service 实现真正的删除功能。
        smbmsUserService.delSmbmsUser(uid);
        HashMap<String, String> resultMap = new HashMap<>();
        resultMap.put("delResult", "true");
        return resultMap;
    }

    @RequestMapping("/jsp/viewuser.do")
    public String viewuser(String method,String uid){
        //对象  ===>HashMap
        System.out.println(method);
        System.out.println(uid);
        SmbmsUser smbmsUser =   smbmsUserService.getSmbmsUserById(uid);
        httpSession.setAttribute("user",smbmsUser);
        if(method.equals("view")){
            return   "userview";
        }else {
            return   "usermodify";
        }
    }
    @RequestMapping("jsp/upduser.do")
    public String upduser(SmbmsUser smbmsUser){
        smbmsUserService.updSmbmsUser(smbmsUser);
        return   "redirect:/jsp/userlist.do";
    }
}
